<template>
	<view class="home">
		<!-- 搜索框 -->
		<view class="top">
			<my-search @click="toSearch" bgc="skyblue" radius="10"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper class="myswiper" indicator-dots autoplay circular interval="2000" duration="200">
			<swiper-item v-for="(item,index) in swiperList" :key="index">
				<navigator :url="item.navigator_url">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 中间的分类区域 -->
		<view class="cate">
			<view @click="toCate(item.name)" class="item" v-for="(item, index) in cateList" :key="index">
				<image :src="item.image_src" mode="aspectFit"></image>
			</view>
		</view>
		<!-- 楼层区域 -->
		<view class="floor">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 标题 -->
				<view class="title">
					<image :src="item.floor_title.image_src" mode="aspectFit"></image>
				</view>
				<!-- 产品 -->
				<view class="prod">
					<view class="left">
						<image @click="toList(item.product_list[0].name)"
							:style="{ width: item.product_list[0].image_width + 'rpx' }"
							:src="item.product_list[0].image_src" mode="">
						</image>
					</view>
					<view class="right">
						<block v-for="(subitem,subindex) in item.product_list">
							<image @click="toList(subitem.name)" :style="{ width: subitem.image_width + 'rpx' }"
								:key="subindex" v-if="subindex !== 0" :src="subitem.image_src" mode=""></image>
						</block>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mymixins from '../../mixins/mymixins.js'
	export default {
		mixins: [mymixins],
		data() {
			return {
				// 获取轮播图数据源
				swiperList: [],
				// 分类的数据源
				cateList: [],
				// 楼层的数据源
				floorList: []
			};
		},
		onLoad() {
			// 获取轮播图的数据
			this.getswiperList()
			// 获取分类的数据
			this.getcateList()
			// 获取楼层数据
			this.getfloorList()
		},
		methods: {
			// 点击搜索框跳转到搜索页
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			// 跳转到列表页面
			toList(name) {
				uni.navigateTo({
					url: `/pages/list/list?key=${name}`
				})
			},
			// 获取轮播图的数据源
			async getswiperList() {
				const res = await uni.$http.get('/api/public/v1/home/swiperdata')
				// console.log(res)
				// 判断返回的状态
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 返回状态为200则保存数据
				this.swiperList = res.data.message
			},
			// 获取分类的数据源
			async getcateList() {
				const res = await uni.$http.get('/api/public/v1/home/catitems')
				// console.log(res)
				// 判断返回状态
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 返回状态为200则保存数据
				this.cateList = res.data.message
			},
			// 跳转到分类
			toCate(name) {
				if (name === '分类') {
					// 跳转到分类页面
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 获取楼层数据源
			async getfloorList() {
				const res = await uni.$http.get('/api/public/v1/home/floordata')
				// console.log(res)
				// 判断返回的状态
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 保存数据
				this.floorList = res.data.message
			}
		}
	}
</script>

<style lang="scss">
	.home {
		.top {
			position: sticky;
			top: 0;
			left: 0;
			z-index: 999;
		}

		.myswiper {
			height: 340rpx;

			image {
				width: 100%;
				height: 340rpx;
			}
		}
	}

	.cate {
		display: flex;
		margin: 20rpx 0;

		.item {
			text-align: center;
			width: 25%;

			image {
				width: 80%;
				height: 140rpx;
			}
		}
	}

	.floor {
		.floor-item {
			.title {
				image {
					width: 100%;
					height: 88rpx;
				}
			}

			.prod {
				display: flex;
				padding: 10rpx 10rpx;

				.left {
					margin-right: 10rpx;

					image {
						height: 400rpx;
					}
				}

				.right {
					image {
						height: 200rpx;
					}

					image:nth-child(1),
					image:nth-child(3) {
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>
